package com.ruoyi.rural.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.rural.domain.RuralHouse;
import com.ruoyi.rural.domain.RuralUser;
import com.ruoyi.rural.domain.RuralUserHouse;
import com.ruoyi.rural.mapper.RuralHouseMapper;
import com.ruoyi.rural.mapper.RuralUserHouseMapper;
import com.ruoyi.rural.mapper.RuralUserMapper;
import com.ruoyi.rural.service.IRuralUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 用户信息Service业务层处理
 * 
 * @author 杨明
 * @date 2022-01-17
 */
@Service
public class RuralUserServiceImpl implements IRuralUserService 
{
    @Autowired
    private RuralUserMapper ruralUserMapper;

    @Autowired
    private RuralUserHouseMapper ruralUserHouseMapper;

    @Autowired
    private RuralHouseMapper ruralHouseMapper;

    /**
     * 查询用户信息
     * 
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public RuralUser selectRuralUserById(String id)
    {
        return ruralUserMapper.selectRuralUserById(id);
    }

    /**
     * 查询用户信息列表
     * 
     * @param ruralUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<RuralUser> selectRuralUserList(RuralUser ruralUser)
    {
        List<RuralUser> ruralUsers = ruralUserMapper.selectRuralUserList(ruralUser);
        for (RuralUser user : ruralUsers) {
            RuralUserHouse ruralUserHouse = new RuralUserHouse();
            ruralUserHouse.setUserId(user.getId());
            List<RuralUserHouse> ruralUserHouses = ruralUserHouseMapper.selectRuralUserHouseList(ruralUserHouse);
            if (!ruralUserHouses.isEmpty()){
                user.setRuralHouseId(ruralUserHouses.get(0).getHouseId());
            }
        }
        return ruralUsers;
    }

    /**
     * 新增用户信息
     * 
     * @param ruralUser 用户信息
     * @return 结果
     */
    @Override
    public int insertRuralUser(RuralUser ruralUser)
    {
        ruralUser.setId(UUID.randomUUID().toString());
        ruralUser.setCreateTime(DateUtils.getNowDate());
        int i = ruralUserMapper.insertRuralUser(ruralUser);
        if (StringUtils.isNotEmpty(ruralUser.getRuralHouseId())){
            RuralUserHouse ruralUserHouse = new RuralUserHouse();
            ruralUserHouse.setHouseId(ruralUser.getRuralHouseId());
            ruralUserHouse.setUserId(ruralUser.getId());
            ruralUserHouse.setId(UUID.randomUUID().toString());
            ruralUserHouseMapper.insertRuralUserHouse(ruralUserHouse);
        }
        return i;
    }

    /**
     * 修改用户信息
     * 
     * @param ruralUser 用户信息
     * @return 结果
     */
    @Override
    public int updateRuralUser(RuralUser ruralUser)
    {
        ruralUser.setUpdateTime(DateUtils.getNowDate());
        if (StringUtils.isNotEmpty(ruralUser.getRuralHouseId())){
            RuralUserHouse ruralUserHouse = new RuralUserHouse();
            ruralUserHouse.setUserId(ruralUser.getId());
            List<RuralUserHouse> ruralUserHouses = ruralUserHouseMapper.selectRuralUserHouseList(ruralUserHouse);
            if (ruralUserHouses.isEmpty()){
                ruralUserHouse.setHouseId(ruralUser.getRuralHouseId());
                ruralUserHouse.setId(UUID.randomUUID().toString());
                ruralUserHouseMapper.insertRuralUserHouse(ruralUserHouse);
            }else {
                if (!ruralUser.getRuralHouseId().equals(ruralUserHouses.get(0).getHouseId())){
                    RuralUserHouse ruralUserHouse1 = ruralUserHouses.get(0);
                    ruralUserHouse1.setHouseId(ruralUser.getRuralHouseId());
                    ruralUserHouseMapper.updateRuralUserHouse(ruralUserHouse1);
                }
            }
        }
        return ruralUserMapper.updateRuralUser(ruralUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteRuralUserByIds(String[] ids)
    {
        return ruralUserMapper.deleteRuralUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteRuralUserById(String id)
    {
        return ruralUserMapper.deleteRuralUserById(id);
    }
}
